var phone=request("phone")
var base64_01="";
var base64_02="";
var base64_03="";
var base64_04="";
//用于压缩图片的canvas
var canvas = document.createElement("canvas");
var ctx = canvas.getContext('2d');
//瓦片canvas
var tCanvas = document.createElement("canvas");
var tctx = tCanvas.getContext("2d");
var maxsize = 100 * 1024;
$(function(){
	//选择图片
	document.getElementById("select_01").onchange = function () {
        get_base64("select_01");
    };
    document.getElementById("select_02").onchange = function () {
        get_base64("select_02");
    };
    document.getElementById("select_03").onchange = function () {
        get_base64("select_03");
    };
    document.getElementById("select_04").onchange = function () {
        get_base64("select_04");
    };
    function get_base64(id) {
        var file = document.getElementById(id).files[0];
        var r = new FileReader();  //本地预览
        r.onload = function(){
        	var base_str="";
	        var img = new Image();
	        img.src = r.result;
        	//如果图片大小小于100kb，则直接添加
	        if (r.result.length <= maxsize) {
	         	base_str=r.result;
	         	img = null;
	         	set_data()
	         	return;
	        }
			//图片加载完毕之后进行压缩
			if (img.complete) {
	          callback();
	        } else {
	          img.onload = callback;
	        }
	        function callback() {
	          var data = compress(img);
	          base_str=data;
	          set_data();
	          img = null;
	        }
        	function set_data(){
        		switch(id){
	        		case "select_01":
	        			base64_01=base_str;
	        		break;
	        		case "select_02":
	        			base64_02=base_str;
	        		break;
	        		case "select_03":
	        			base64_03=base_str;
	        		break;
	        		case "select_04":
	        			base64_04=base_str;
	        		break;
	        	}
        		$("#"+id).parents(".certificates").find(".pic img").attr("src",base_str)
        		console.log(base_str)
        	}
        }
        r.readAsDataURL(file);    //Base64
    }
    
	//    使用canvas对大图片进行压缩
	function compress(img) {
	    var initSize = img.src.length;
	    var width = img.width;
	    var height = img.height;
	    //如果图片大于四百万像素，计算压缩比并将大小压至400万以下
	    var ratio;
	    if ((ratio = width * height / 4000000) > 1) {
	      ratio = Math.sqrt(ratio);
	      width /= ratio;
	      height /= ratio;
	    } else {
	      ratio = 1;
	    }
	    canvas.width = width;
	    canvas.height = height;
	//        铺底色
	    ctx.fillStyle = "#fff";
	    ctx.fillRect(0, 0, canvas.width, canvas.height);
	    //如果图片像素大于100万则使用瓦片绘制
	    var count;
	    if ((count = width * height / 1000000) > 1) {
	      count = ~~(Math.sqrt(count) + 1); //计算要分成多少块瓦片
	//            计算每块瓦片的宽和高
	      var nw = ~~(width / count);
	      var nh = ~~(height / count);
	      tCanvas.width = nw;
	      tCanvas.height = nh;
	      for (var i = 0; i < count; i++) {
	        for (var j = 0; j < count; j++) {
	          tctx.drawImage(img, i * nw * ratio, j * nh * ratio, nw * ratio, nh * ratio, 0, 0, nw, nh);
	          ctx.drawImage(tCanvas, i * nw, j * nh, nw, nh);
	        }
	      }
	    } else {
	      ctx.drawImage(img, 0, 0, width, height);
	    }
	    //进行最小压缩
	    var ndata = canvas.toDataURL('image/jpeg', 0.1);
	    console.log('压缩前：' + initSize);
	    console.log('压缩后：' + ndata.length);
	    console.log('压缩率：' + ~~(100 * (initSize - ndata.length) / initSize) + "%");
	    tCanvas.width = tCanvas.height = canvas.width = canvas.height = 0;
	    return ndata;
	}
	$("#btn_01").click(function(){
		if(base64_01==""){
			return false;
		}
		upload("1",encodeURIComponent(base64_01))
	})
	$("#btn_02").click(function(){
		if(base64_02==""){
			return false;
		}
		upload("2",encodeURIComponent(base64_02))
	})
	$("#btn_03").click(function(){
		if(base64_03==""){
			return false;
		}
		upload("3",encodeURIComponent(base64_03))
	})
	$("#btn_04").click(function(){
		if(base64_04==""){
			return false;
		}
		upload("4",encodeURIComponent(base64_04))
	})
	//上传
	function upload(pic_type,base64str){
		$.ajax({
		    url: DATA_URL+"upsharepic",
		    data: "phone="+phone+"&pic_type="+pic_type+"&base64str="+base64str,
		    type: "post",
		    dataType: 'json',
		    timeout: 5000,
		    success:function (data) {
		    	if(data.code=="0"){
		    		$("#prompt").text("上传成功");
		            $(".prompt").fadeIn(300,function(){
		                setTimeout(function(){$(".prompt").fadeOut(300)},3000)
		            })
					switch(pic_type){
						case "1":
							base64_01="";
						break;
						case "2":
							base64_02="";
						break;
						case "3":
							base64_03="";
						break;
						case "4":
							base64_04="";
						break;
					}
		    	}
				else{
					$("#prompt").text(data.msg);
		            $(".prompt").fadeIn(300,function(){
		                setTimeout(function(){$(".prompt").fadeOut(300)},3000)
		            })
				}
		    },
		    error:function () {
		    	$("#prompt").text("您的网络不给力哦~");
		        $(".prompt").fadeIn(300,function(){
		            setTimeout(function(){$(".prompt").fadeOut(300)},3000)
		        })
		    }
		});
	}
	//获取图片
	get_pic();
	function get_pic(){
		$.ajax({
		    url: DATA_URL+"getsharepic",
		    data: "phone="+phone,
		    type: "post",
		    dataType: 'json',
		    timeout: 5000,
		    success:function (data) {
		    	if(data.code=="0"){
		    		$("#pic_01").attr("src",data.info.share_pic+"?time="+new Date().getTime());
		    		$("#pic_02").attr("src",data.info.couch_share_pic+"?time="+new Date().getTime())
		    		$("#pic_03").attr("src",data.info.bottom_pic+"?time="+new Date().getTime());
		    		$("#pic_04").attr("src",data.info.couch_bottom_pic+"?time="+new Date().getTime())
		    	}
				else{
					$("#prompt").text(data.msg);
		            $(".prompt").fadeIn(300,function(){
		                setTimeout(function(){$(".prompt").fadeOut(300)},3000)
		            })
				}
		    },
		    error:function () {
		    	$("#prompt").text("您的网络不给力哦~");
		        $(".prompt").fadeIn(300,function(){
		            setTimeout(function(){$(".prompt").fadeOut(300)},3000)
		        })
		    }
		});
	}
})
